<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Controller;
use App\Models\Option;
use Illuminate\Http\Request;
class OptionController extends Controller{

    public function index(Request $request,$symbol){
        //$res = get_settle_option_data('HIHSI10','2016-10-24 17:33:25');
        //var_dump($res);
        $user = $request->user();
        $option = Option::select('id','name', 'code','times','currency_name','currency_unit', 'exchange_rate',
        'fluctuation',   //最小波动值
        'fluctuation_price', //最小波动价格
        'surplus_value',    //止盈值
        'stop_loss_value',  //止损值
        'stop_loss_margin', //止损保证金
        'transaction_cost', //综合交易费
        'hand_count')->where('code','=',$symbol)->first();
        if(!$option){
            return redirect('/');
        }
        //get_option_data('PMAU');
        $times = $option->times;
        
        $times = explode(',',$times);

        //判断是否是在交易时间
        // $times = $option->times;
        // $times = explode(',',$times);
        $timesWhere = false;
        $endDateTime ='';
        foreach($times as $k=>$time){
            list($start,$end) = explode('-',$time);
            if($start>=$end){
                if(date("w")!=6){
                    if(date('H:i')>=$start || date('H:i',time())<=$end){
                        $timesWhere = true;
                        if(date('H:i')>=$start){
                            //mktime(hour,minute,second,month,day,year,is_dst);
                            //$endDateTime = mktime(date('H',strtotime($end)),date('i',strtotime($end)),date('s',strtotime($end)),date('m'),date('d',strtotime("+1 day")),date('Y'));
                            $endDateTime = $end;
                        }else{
                            // $endDateTime = mktime(date('H',strtotime($end)),date('i',strtotime($end)),date('s',strtotime($end)),date('m'),date('d'),date('Y'));
                            $endDateTime = $end;
                        }
                        break;return;
                    }
                }else{
                    if(date('H:i',time())<=$end){
                        $timesWhere = true;
                        // $endDateTime = mktime(date('H',strtotime($end)),date('i',strtotime($end)),date('s',strtotime($end)),date('m'),date('d'),date('Y'));
                        $endDateTime = $end;
                        break;return;
                    }
                }
                
            }else{
                if(date('H:i')>=$start && date('H:i',time())<=$end){
                    $timesWhere = true;
                    //$endDateTime = mktime(date('H',strtotime($end)),date('i',strtotime($end)),date('s',strtotime($end)),date('m'),date('d'),date('Y'));
                    $endDateTime = $end;
                    break;return;
                }else{
                    
                }
            }
        }
        //

        $newtimes = [];
        foreach($times as $k=>$time){
            list($start,$end) = explode('-',$time);
            if($start>=$end){
                $periodone = $start.'-24:00';
                $periodtwo = "00:00-".$end;
                //unset($times[$k]);
                $newtimes[] = $periodone;
                $newtimes[] = $periodtwo;
            }else{
                $newtimes[] = $time;
            }
        }
        //$times =implode(',',$times);
        $newtimes = json_encode($newtimes);
        //$newtimes =implode(',',$newtimes);
        $optionData['date_time'] = 0;
        $optionData['create_time'] = 0;
        $optionData= get_option_data($symbol);
        if(!empty($optionData)){
            @$optionDate = date('Ymd',strtotime($optionData['date_time']));
        }
        
        //echo $optionDate;
        if(date('Ymd')!=$optionDate || strtotime($optionData['date_time'])<$optionData['create_time']-20){
            $endDateTime = '';
            
        }
        $otherOptions = Option::select('id','name', 'code','times','currency_name','currency_unit', 'exchange_rate',
        'fluctuation',   //最小波动值
        'fluctuation_price', //最小波动价格
        'surplus_value',    //止盈值
        'stop_loss_value',  //止损值
        'stop_loss_margin', //止损保证金
        'transaction_cost', //综合交易费
        'hand_count')->where('code','!=',$symbol)->where('status','=',1)->get();

        return view('home.option.index',compact('user','option','newtimes','optionData','otherOptions','endDateTime'));
    }

    /**
     * [rules 交易规则]
     * @param  Request $request [description]
     * @param  [type]  $symbol  [description]
     * @return [type]           [description]
     */
    public function rules(Request $request,$symbol){
         $option = Option::where('code','=',$symbol)->first();
         return view('home.option.rules',compact('option'));
    }


}